const news = {
  namespaced: true,

  state: {
    isLogin: false,
    isSDKReady: false, // TIM SDK 是否 ready
    user: '',
    img: '',
    userId: '',
    userSig: '',
    // userId: 'user001', //本人id
    // userSig: 'eJwtzLEOgjAUheF36arB29KikjDIIINMYjQSF5IWvQEFWookxncXgfF8f3I*5BQnTqc08QlzgCzHjVK9WsxxZGuUBqBzMrLI6hol8SkH4BzYhk5F9TVqNbgQggHApC0*-*aByxms*awG78Ozt3B3IMrtvrqtmiI6Wxnl796GPDHHuEyzzuDjEqomPVyrgHx-g0UyVQ__', //本人签名
    conversationActive: {}, //聊天进行中的会话
    toUserId: '', //聊天对象id
    toUserInfo: {},
    conversationList: [], //会话列表
    currentMessageList: [], //消息列表
    backFlag: false,
    order: {
      user: '',
      price: '',
      pageId: '',
      name: '',
      tab: '',
    },
    cardInfo: {
      //卡片信息
    },
  },

  mutations: {
    //更新登录状态
    toggleIsLogin(state, isLogin) {
      state.isLogin = typeof isLogin === 'undefined' ? !state.isLogin : isLogin
    },

    //更新TIMSDK状态
    toggleIsSDKReady(state, isSDKReady) {
      state.isSDKReady =
        typeof isSDKReady === 'undefined' ? !state.isSDKReady : isSDKReady
    },

    //退出登录重置状态
    reset(state) {
      state.isLogin = false
      state.isSDKReady = false
    },

    //保存当前用户tim信息
    setUer(state, data) {
      state.img = data.img
      state.user = data.user
      state.userId = data.userId
      state.userSig = data.userSig
    },

    //选择好友聊天--创建会话/拼接会话id
    createConversationActive(state, data) {
      console.log('createConversationActive:', data)
      state.toUserInfo = data
      state.conversationActive.conversationID = 'C2C' + data.userId
      state.toUserId = data.userId
      state.currentMessageList = []
    },
    //选择已有会话聊天--更新选中会话详情
    updateConversationActive(state, conversationItem) {
      console.log(JSON.stringify(conversationItem))
      state.conversationActive = Object.assign(
        {},
        conversationItem.conversation
      )
      state.toUserId = conversationItem.user.userId
      state.currentMessageList = []
    },
    //更新会话列表
    updateConversationList(state, newConversationList) {
      state.conversationList = newConversationList
    },
    /**
     * 将消息插入当前会话列表
     * 调用时机：收/发消息事件触发时
     * @param {Object} state
     * @param {Message[]|Message} data
     * @returns
     */
    pushCurrentMessageList(state, data) {
      // 还没当前会话，则跳过
      if (Array.isArray(data)) {
        // 筛选出当前会话的消息
        const result = data.filter(
          (item) =>
            item.conversationID === state.conversationActive.conversationID
        )
        state.currentMessageList = [...state.currentMessageList, ...result]
      } else if (
        data.conversationID === state.conversationActive.conversationID
      ) {
        state.currentMessageList = [...state.currentMessageList, data]
      }
    },
    /**
     * 滑到顶部请求更多的历史消息
     * */
    unshiftCurrentMessageList(state, data) {
      if (data) {
        state.currentMessageList = [...data, ...state.currentMessageList]
      }
    },

    backRoom(state, data) {
      state.backFlag = data.backFlag
      // state.order.user = data.user
      // state.order.name = data.name
      // state.order.price = data.price
      // state.order.pageId = data.pageId
      state.order.tab = data.tab
    },

    retBackFlag(state) {
      state.backFlag = false
    },

    setCardInfo(state, data) {
      state.cardInfo = data
    },
  },

  actions: {},
}
export default news
